package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Location;
import com.zyd.shiro.business.service.PakLocationService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakLocation;
import com.zyd.shiro.persistence.mapper.PakLocationMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakLocationServiceImpl implements PakLocationService {

    @Autowired
    private PakLocationMapper pakLocationMapper;

    @Override
    public Location insert(Location location) {
        Assert.notNull(location, "对象不可为空！");
        location.getPakLocation().setUpdateTime(new Date());
        location.getPakLocation().setCreateTime(new Date());
        pakLocationMapper.insertSelective(location.getPakLocation());
        return location;
    }

    @Override
    public void insertList(List<Location> locations) {
            for(Location location:locations){
               location.getPakLocation().setUpdateTime(new Date());
               location.getPakLocation().setCreateTime(new Date());
               pakLocationMapper.insertSelective(location.getPakLocation());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakLocationMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Location location) {
        Assert.notNull(location, "对象不可为空！");
        location.setUpdateTime(new Date());
        return pakLocationMapper.updateByPrimaryKey(location.getPakLocation()) > 0;
    }

    @Override
    public boolean updateSelective(Location location) {
        Assert.notNull(location, "对象不可为空！");
        location.setUpdateTime(new Date());
        return pakLocationMapper.updateByPrimaryKeySelective(location.getPakLocation()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Location getByPrimaryKey(Long primaryKey) {
        PakLocation pakLocation = pakLocationMapper.selectByPrimaryKey(primaryKey);
        return null == pakLocation ? null : new Location(pakLocation);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Location getOneByEntity(Location test) {
        Assert.notNull(test, "对象不可为空！");
        PakLocation pakLocation = pakLocationMapper.selectOne(test.getPakLocation());
        return null == pakLocation ? null : new Location(pakLocation);
    }

    @Override
    public List<Location> listAll() {
        List<PakLocation> pakLocations = pakLocationMapper.selectAll();
        if (CollectionUtils.isEmpty(pakLocations)) {
            return null;
        }
        List<Location> locations = new ArrayList<>();
        for (PakLocation pakLocation : pakLocations) {
            locations.add(new Location(pakLocation));
        }
        return locations;
    }

    @Override
    public List<Location> listByEntity(Location entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Location> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakLocation> pakLocations = pakLocationMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakLocations)) {
            return null;
        }
        List<Location> locations = new ArrayList<>();
        for (PakLocation pakLocation : pakLocations) {
            locations.add(new Location(pakLocation));
        }
        PageInfo bean = new PageInfo<PakLocation>(pakLocations);
        bean.setList(locations);
        return bean;
    }
    @Override
    public void increase(PakLocation pakLocation) {
        pakLocationMapper.insert(pakLocation);
    }

    @Override
    public List<PakLocation> listByDeptId(long deptId) {
        return pakLocationMapper.findAllByDept(deptId);
    }


}
